/**
 * @Title SysStartServiceImpl.java
 * @Package com.eppear.jpfm.framework.service
 * @Description
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-22 下午9:05:05
 * @version 
 */
package com.eppear.jpfm.framework.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eppear.jpfm.framework.constants.Constants;
import com.eppear.jpfm.framework.context.Context;
import com.eppear.jpfm.framework.model.TSysUser;
import com.eppear.jpfm.framework.model.TWxAccount;
import com.eppear.jpfm.framework.service.SysStartService;

import tk.mybatis.mapper.common.Mapper;

/**
 * @ClassName SysStartServiceImpl
 * @Description 系统启动时的服务层,用于缴存一些全局数据
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-22 下午9:05:05
 *
 */
@Service("sysStartService")
public class SysStartServiceImpl extends BaseService<TSysUser> implements SysStartService {

	private final Logger log = LoggerFactory.getLogger(SysStartServiceImpl.class);

	@Autowired
	protected Mapper<TWxAccount> wxAccmapper;

	@Override
	public void initCache() {
		reloadDic();
		reloadSysParm();
		reloadUsers();
		reloadWxUser();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.eppear.jpfm.framework.service.SysStartService#reloadDic()
	 */
	@Override
	public void reloadDic() {
		log.info("-----------------初始化字典缓存开始---------------------------");
		Map<Object, Object>  cache = (null == Context.getCach() ? new HashMap<Object, Object>() : Context.getCach()) ;
		Map<String, Object> dicCache = new ConcurrentHashMap<String, Object>();// 线程安全MAP
		List<Map> dicTypeList = this.selectFromXML("system.selDicTypeList", null);// 查询字典类型集合
		for (int i = 0; i < dicTypeList.size(); i++) {
			Map dicType = dicTypeList.get(i);
			Integer dicTypeID = (Integer) dicType.get("ID");
			String dicTypeCode = (String) dicType.get(Constants.DIC_TYPE_CODE);// 字典CODE
			Map<String, Object> qryDicDataMap = new HashMap<String, Object>();
			qryDicDataMap.put(Constants.DIC_TYPE_ID, dicTypeID);
			List<Map> dicDataList = this.selectFromXML("system.selDicDataList", qryDicDataMap);// 查询字典类型集合
			Map dic = new HashMap();
			for (int j = 0; j < dicDataList.size(); j++) {
				Map dicData = dicDataList.get(j);
				dic.put(dicData.get(Constants.DIC_VALUE), dicData.get(Constants.DIC_NAME));
			}
			dicCache.put(dicTypeCode, dic);
			dicCache.put(dicTypeCode + "List", dicDataList);
		}
		cache.put(Constants.CACHE_DIC_KEY, dicCache);
		Context.setCache(cache);
		log.info("-----------------初始化字典缓存结束---------------------------");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.eppear.jpfm.framework.service.SysStartService#reloadSysParm()
	 */
	@Override
	public void reloadSysParm() {
		log.info("-----------------初始化参数缓存开始---------------------------");
		Map<Object, Object>  cache = (null == Context.getCach() ? new HashMap<Object, Object>() : Context.getCach()) ;
		List<Map> paramDataList = this.selectFromXML("system.selParamDataList", null);
		Map paramCache = new ConcurrentHashMap(paramDataList.size());// 线程安全MAP
		for (int i = 0; i < paramDataList.size(); i++) {
			Map param = paramDataList.get(i);
			paramCache.put(param.get(Constants.PARM_KEY), param.get(Constants.PARM_VALUE));
		}

		cache.put(Constants.CACHE_SYS_PARAM_KEY, paramCache);
		Context.setCache(cache);
		log.info("-----------------初始化参数缓存结束---------------------------");

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.eppear.jpfm.framework.service.SysStartService#reloadUsers()
	 */
	@Override
	public void reloadUsers() {		
		log.info("-----------------初始化人员缓存开始---------------------------");
		Map<Object, Object>  cache = (null == Context.getCach() ? new HashMap<Object, Object>() : Context.getCach()) ;
		List<TSysUser> adminList = this.mapper.selectAll();
		Map<String, String> adminListCache = new ConcurrentHashMap<String, String>(adminList.size());// 线程安全MAP
		for (int i = 0; i < adminList.size(); i++) {
			TSysUser admin = adminList.get(i);
			adminListCache.put(String.valueOf(admin.getId()), (admin.getName() == null ? "" : admin.getName()));
		}
		cache.put(Constants.OPER_LIST_CACHE_KEY, adminListCache);
		Context.setCache(cache);
		log.info("-----------------初始化人员缓存结束---------------------------");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.eppear.jpfm.framework.service.SysStartService#reloadWxUser()
	 */
	@Override
	public void reloadWxUser() {
		log.info("-----------------初始化微信账户信息开始---------------------------");
		Map<Object, Object>  cache = (null == Context.getCach() ? new HashMap<Object, Object>() : Context.getCach()) ;
		List<TWxAccount> wxAccList = this.wxAccmapper.selectAll();
		Map<Integer, TWxAccount> wxAccs = new ConcurrentHashMap<Integer, TWxAccount>(wxAccList.size());// 线程安全MAP
		for (int i = 0; i < wxAccList.size(); i++) {
			TWxAccount wxAcc = wxAccList.get(i);
			wxAccs.put(wxAcc.getId(), wxAcc);
		}
		cache.put(Constants.WX_ACCOUNT_LISTMAP, wxAccs);
		Context.setCache(cache);
		log.info("-----------------初始微信账户信息结束---------------------------");
	}
}
